package com.bytedance.scenedemo.extreme_case;

import com.bytedance.scene.Scene;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.interfaces.ChildSceneLifecycleCallbacks;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.util.ResUtil;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by JiangQi on 9/5/18.
 */
public class Case5Scene extends GroupScene implements ChildSceneLifecycleCallbacks{
    private StringBuilder stringBuilder = new StringBuilder();
    private Text textHint;
    private DirectionalLayout directionalLayout;
    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        return (ComponentContainer) inflater.parse(ResourceTable.Layout_case4_scene_layout,container,false);
    }

    @Override
    public void onViewCreated(Component layout, Intent intent) {
        super.onViewCreated(layout, intent);
        textHint = (Text) layout.findComponentById(ResourceTable.Id_text_case4scene_hint);
        directionalLayout = (DirectionalLayout) layout.findComponentById(ResourceTable.Id_top_layout);
        directionalLayout.setHeight(500);
        DirectionalLayout svMain = (DirectionalLayout) layout.findComponentById(ResourceTable.Id_sv_push_pop_basic_usage_main);
        int value = 0;
        if(getArguments()!=null){
            value= (int) getArguments().getParam("value");
        }
        svMain.setBackground(ColorUtil.getMaterialColor(value));

        Button btnPushAndPop1 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_1);
        btnPushAndPop1.setText(ResUtil.getString(getSceneContext(),ResourceTable.String_case_group_scene_btn_1));
        btnPushAndPop1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Scene scene = findSceneByTag("11");
                if (scene != null) {
                    remove(scene);
                } else {
                    GroupScene testGroupScene = new TestGroupScene();
                    IntentParams intentParams = new IntentParams();
                    intentParams.setParam("value",2);

                    testGroupScene.add(ResourceTable.Id_group_id1, EmptyScene1.newInstance(2), "0");
                    testGroupScene.add(ResourceTable.Id_group_id2, EmptyScene2.newInstance(3), "1");
                    add(ResourceTable.Id_top_layout, testGroupScene, "11");
                }
            }
        });

        GroupScene testGroupScene = new TestGroupScene();
        testGroupScene.add(ResourceTable.Id_group_id1, EmptyScene1.newInstance(2), "0");
        testGroupScene.add(ResourceTable.Id_group_id2, EmptyScene2.newInstance(3), "1");

        Button btnPushAndMany2 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_2);
        btnPushAndMany2.setText(ResUtil.getString(getSceneContext(),ResourceTable.String_case_group_scene_btn_2));
        btnPushAndMany2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Scene scene = findSceneByTag("11");
                if (scene != null) {
                    remove(scene);
                } else {
                    add(ResourceTable.Id_top_layout, testGroupScene, "11");
                }
            }
        });
        SceneExtensions.requireNavigationScene(Case5Scene.this).registerChildSceneLifecycleCallbacks(this,true);
    }

    private void ddd() {
        textHint.setText(stringBuilder.toString());
        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                textHint.scrollTo(0, 0);
            }
        }, 10);
    }

    @Override
    public void onAbilityCreated(Intent intent) {
        super.onAbilityCreated(intent);
    }

    @Override
    public void onSceneCreated(Scene scene, Intent intent) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onCreated");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneViewCreated(Scene scene, Intent intent) {

    }

    @Override
    public void onSceneAbilityCreated(Scene scene, Intent intent) {

    }

    @Override
    public void onSceneStarted(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onStart");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneResumed(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onResume");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneSaveInstanceState(Scene scene, Intent intent) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onSaveInstanceState");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onScenePaused(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onPause");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneStopped(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onStop");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneViewDestroyed(Scene scene) {

    }

    @Override
    public void onSceneDestroyed(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onDestroy");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}